//
//  SetPushMessageViewController.m
//  ZhuiKe55like
//
//  Created by junseek on 16/8/31.
//  Copyright © 2016年 五五来客 李江. All rights reserved.
//

#import "SetPushMessageViewController.h"
#import "NetEngine.h"

@interface SetPushMessageViewController ()<UIAlertViewDelegate>{
    NSDictionary *dicPush;
    
    NSInteger tempTag;
    UIScrollView *scrollViewBG;
}

@end

@implementation SetPushMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [NetEngine createHttpAction:XKFV3usersendstatus withCache:NO withParams:@{@"uid":[[Utility Share] userId],@"token":[[Utility Share] userToken]} withMask:SVProgressHUDMaskTypeClear onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            dicPush=[resData objectForJSONKey:@"data"];
            [self initComponents];
        }else{
            [self performSelector:@selector(backButtonClicked:) withObject:nil afterDelay:0.8];
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        }
    } onError:^(NSError *error) {
        [self performSelector:@selector(backButtonClicked:) withObject:nil afterDelay:0.8];
        [SVProgressHUD showImage:nil status:alertErrorTxt];
    }];
}
-(void)initComponents{
    scrollViewBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, H(self.view)- (kVersion7?0:44))];
    [scrollViewBG setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [scrollViewBG setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:scrollViewBG];
    
    float fy=10;
    UILabel *lbltitle=[RHMethods labelWithFrame:CGRectMake(10, fy, kScreenWidth-20, 0) font:fontSmallTitle color:rgbTxtGray text:@"如果您不想接收推送，请关闭对应栏目"];
    [scrollViewBG addSubview:lbltitle];
    fy+=H(lbltitle)+10;
    UIView *viewBg=[[UIView alloc] initWithFrame:CGRectMake(0, fy, kScreenWidth, 44)];
    [scrollViewBG addSubview:viewBg];
    [viewBg setBackgroundColor:[UIColor whiteColor]];
    
    [scrollViewBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, fy+12, 0, 20) font:fontTitle color:rgbTitleColor text:@"牌照提醒"]];
    [scrollViewBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, fy, kScreenWidth, 0.5) defaultimage:@"userLine"]];
    UISwitch *switchButton2 = [[UISwitch alloc] initWithFrame:CGRectMake(kVersion7?(kScreenWidth-10)-51:(kScreenWidth-10)-79, fy+( kVersion7?6:8), kVersion7?51:79,kVersion7? 30:27)];
    [switchButton2 setOnTintColor:rgbpublicColor];
    [switchButton2 setTag:101];
    [switchButton2 addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [scrollViewBG addSubview:switchButton2];
    switchButton2.on=[[dicPush valueForJSONStrKey:@"productpush"] isEqualToString:@"1"];
    fy+=44;
    [scrollViewBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, fy-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
    
    [scrollViewBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, fy+12, 0, 20) font:fontTitle color:rgbTitleColor text:@"通知提醒"]];
    UISwitch *switchButton3 = [[UISwitch alloc] initWithFrame:CGRectMake(kVersion7?(kScreenWidth-10)-51:(kScreenWidth-10)-79, fy+( kVersion7?6:8), kVersion7?51:79,kVersion7? 30:27)];
    [switchButton3 setOnTintColor:rgbpublicColor];
    [switchButton3 setTag:102];
    [switchButton3 addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [scrollViewBG addSubview:switchButton3];
    switchButton3.on=[[dicPush valueForJSONStrKey:@"noticepush"] isEqualToString:@"1"];
    fy+=44;
    [scrollViewBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, fy-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
    
    [scrollViewBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, fy+12, 0, 20) font:fontTitle color:rgbTitleColor text:@"项目提醒"]];
    UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(kVersion7?(kScreenWidth-10)-51:(kScreenWidth-10)-79, fy+( kVersion7?6:8), kVersion7?51:79,kVersion7? 30:27)];
    [switchButton setOnTintColor:rgbpublicColor];
    [switchButton setTag:103];
    [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [scrollViewBG addSubview:switchButton];
    switchButton.on=[[dicPush valueForJSONStrKey:@"projectpush"] isEqualToString:@"1"];
    fy+=44;
    [scrollViewBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, fy-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
   
    
    [scrollViewBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, fy+12, 0, 20) font:fontTitle color:rgbTitleColor text:@"供求提醒"]];
    UISwitch *switchButton4 = [[UISwitch alloc] initWithFrame:CGRectMake(kVersion7?(kScreenWidth-10)-51:(kScreenWidth-10)-79, fy+( kVersion7?6:8), kVersion7?51:79,kVersion7? 30:27)];
    [switchButton4 setOnTintColor:rgbpublicColor];
    [switchButton4 setTag:104];
    [switchButton4 addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [scrollViewBG addSubview:switchButton4];
    switchButton4.on=[[dicPush valueForJSONStrKey:@"demandpush"] isEqualToString:@"1"];
    fy+=44;
    
    
    viewBg.frameHeight=fy-Y(viewBg);
    [scrollViewBG setContentSize:CGSizeMake(1, YH(viewBg)+20)];
    
}

-(void)switchAction:(UISwitch *)sw{
    tempTag=sw.tag;
    BOOL abool=sw.on;
    if (abool) {
        [self setPushData];
    }else{
        if (sw.tag==101) {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"关闭之后您将无法收到【牌照提醒】消息，是否关闭？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            [alert show];
        }else if (sw.tag==102){
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"关闭之后您将无法收到【通知提醒】消息，是否关闭？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            [alert show];
        }else if (sw.tag==103){
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"关闭之后您将无法收到【项目提醒】消息，是否关闭？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            [alert show];
        }else if (sw.tag==104){
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"系统提示" message:@"关闭之后您将无法收到【供求提醒】消息，是否关闭？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            [alert show];
        }
    }
    
    
}
-(void)setPushData{
    UISwitch *sw=[scrollViewBG viewWithTag:tempTag];
    
    BOOL abool=sw.on;
    NSMutableDictionary *dit=[[NSMutableDictionary alloc] init];
    [dit setValue:[[Utility Share] userId] forKey:@"uid"];
    [dit setValue:[[Utility Share] userToken] forKey:@"token"];
    if (sw.tag==101) {//属性设置 1牌照推送 2 供求评论推送 3项目推送    4公告推送
        [dit setValue:@"1" forKey:@"type"];
    }else if (sw.tag==102){
        [dit setValue:@"4" forKey:@"type"];
    }else if (sw.tag==103){
        [dit setValue:@"3" forKey:@"type"];
    }else if (sw.tag==104){
        [dit setValue:@"2" forKey:@"type"];
    }
    [dit setValue:abool?@"1":@"0" forKey:@"val"];
    
    
    [NetEngine createHttpAction:XKFV3usersetsend withCache:NO withParams:dit withMask:SVProgressHUDMaskTypeNone onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            
        }else{
            if (sw) {
                sw.on=!abool;
            }
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        }
    } onError:^(NSError *error) {
        if (sw) {
            sw.on=!abool;
        }
        [SVProgressHUD showImage:nil status:alertErrorTxt];
    }];
}
#pragma mark UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        UISwitch *sw=[scrollViewBG viewWithTag:tempTag];
        sw.on=YES;
    }else{
        [self setPushData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
